$.extend({
    CommonAdmin: {
        //region PageInit:初始化页面
        PageInit: function () {
            //PageInit
            $.Common.PageInit();
            //Bootstrap
            $.Common.Bootstrap.PageInit();
            //权限检查
            this.Permission.CheckElement();
            //绑定数据项
            $.CommonAdmin.DataBind.fnBind();
            //打开相关窗口
            $(document).delegate('[data-openwindow]', 'click', function (e) {
                $.CommonAdmin.OpenWindow({
                    ID: $(this).data('openwindow'),
                    Element: this,
                    Event: e
                });
            });
        },
        //endregion
        //region DataBind:数据绑定
        DataBind: {
            //region fnBind
            fnBind: function (element) {
                if (!element) {
                    element = $('body');
                }
                if (!(element instanceof jQuery)) {
                    element = $(element);
                }
                if (element.attr('data-databind') && element.attr('data-databind') == 'true') {
                    return;
                }
                var _element;
                if (element.attr('data-databind')) {
                    _element = [element];
                }
                else {
                    _element = element.find('[data-databind][data-databind!="true"]');
                }
                $(_element).each(function () {
                    switch ($(this).attr('data-databind')) {
                        case 'Family'://公会
                            $.CommonAdmin.DataBind.Family.fnBind(this);
                            break;
                    }
                });
            },
            //endregion
            //region Family
            Family: {
                Data: [],
                fnInfo: function (id) {
                    return _.find(this.Data, {id: id});
                },
                fnBindSelect2: function (element) {
                    if (element.prop('disabled')) {
                        element.select2();
                        return;
                    }
                    element.select2({
                        language: 'zh-CN',
                        ajax: {
                            url: '/ajax/user.family-info.list',
                            dataType: 'json',
                            delay: 500,
                            data: function (params) {
                                return {
                                    FuzzyQuery: params.term
                                };
                            },
                            processResults: function (data) {
                                $.CommonAdmin.DataBind.Family.Data = _.union($.CommonAdmin.DataBind.Family.Data, data.data);
                                return {
                                    results: data.data
                                };
                            },
                            cache: true
                        },
                        escapeMarkup: function (markup) {
                            return markup;
                        },
                        minimumInputLength: 1,
                        templateResult: function (repo) {
                            if (repo.loading) return repo.text;
                            return (repo.Status != 0 ? "[锁]" : "") + repo.Family_Name;
                        },
                        templateSelection: function (repo) {
                            return repo.Family_ID ? (repo.Status != 0 ? "[锁]" : "") + repo.Family_Name : repo.text;
                        }
                    });
                },
                fnBind: function (element) {
                    if (!element) {
                        return;
                    }
                    if (!(element instanceof jQuery)) {
                        element = $(element);
                    }
                    var _parms = {
                        DefaultValue: $.KOC.String.ToInt(element.attr('data-databind-default'), -1),
                        DefaultText: element.attr('data-databind-default-text'),
                        Placeholder: element.attr('placeholder') || '公会查询…',
                        Disabled: element.attr('disabled')
                    };
                    if (element.attr('data-databind') === 'true') {
                        if (!_parms.DefaultValue) {
                            return;
                        }
                        element.select2("destroy");
                    }
                    element.attr({
                        'data-databind': true,
                        'data-placeholder': _parms.Placeholder
                    }).empty();
                    if (_parms.DefaultValue && $.KOC.String.ToIntPositive(_parms.DefaultValue)) {
                        element.removeAttr('data-databind-default');
                        if (!_parms.DefaultText) {
                            $.ajax({
                                url: '/ajax/user.family-info.list',
                                type: 'post',
                                dataType: 'json',
                                data: {
                                    Family_ID: _parms.DefaultValue
                                },
                                success: function (data) {
                                    if (data.flag !== 0 || data.data.length <= 0) {
                                        return;
                                    }
                                    data = data.data;
                                    $.CommonAdmin.DataBind.Family.Data = _.union($.CommonAdmin.DataBind.Family.Data, data);
                                    data = data[0];
                                    element.append($("<option/>", {
                                        value: _parms.DefaultValue,
                                        text: (data.Status != 0 ? "[锁]" : "") + data.Family_Name
                                    }));
                                    $.CommonAdmin.DataBind.Family.fnBindSelect2(element);
                                }
                            });
                        } else {
                            element.append($("<option/>", {
                                value: _parms.DefaultValue,
                                text: _parms.DefaultText
                            }));
                            this.fnBindSelect2(element);
                        }
                    } else {
                        this.fnBindSelect2(element);
                    }
                }
            }
            //endregion
        },
        //endregion
        //region OpenWindow:打开相关窗口
        OpenWindow: function (parm, onEnter, onClose) {
            var _parm = {
                Info: false,
                ID: null,
                Type: null,
                URL: '',
                Parm: '',
                RightCode: '',
                Modal: true,
                Element: null,
                Event: null,
                onEnter: onEnter,
                onClose: onClose
            };
            switch (typeof parm) {
                case 'string':
                    _parm.ID = parm;
                    break;
                case 'object':
                    _parm.ID = String(parm.ID);
                    _parm.Element = parm.Element;
                    _parm.Event = parm.Event;
                    break;
                default:
                    $.KOC.Tips.Open('Error', '传入参数错误', {Timeout: 1000});
                    return;
            }
            if (!_parm.ID) {
                $.KOC.Tips.Open('Error', '传入参数错误', {Timeout: 1000});
                return;
            }
            if (_parm.ID.indexOf('[Info]') == 0) {
                _parm.Info = true;
                _parm.ID = _parm.ID.substr(6);
            }
            var _parmValue = _parm.ID.split('|');
            for (var i = 0; i < _parmValue.length; i++) {
                var ThisValue = _parmValue[i].split(':');
                switch (ThisValue.length) {
                    case 1:
                        _parm.Type = ThisValue[0].substr(6, 2);
                        _parm.ID = ThisValue[0];
                        break;
                    case 2:
                        if (_parm.Parm) {
                            _parm.Parm += '&';
                        }
                        _parm.Parm += ThisValue[0] + '=' + ThisValue[1];
                        break;
                    case 3:
                        _parm.Type = ThisValue[1];
                        _parm.ID = ThisValue[2];
                        break;
                }
            }
            switch (_parm.Type) {
                case 'UserInfo'://用户信息
                    _parm.URL = '/user-manage/base-manage/user-info/' + _parm.ID;
                    _parm.RightCode = '{User}[Base](Info)';
                    break;
                case 'UserEdit'://用户编辑
                    _parm.URL = '/user-manage/base-manage/user-edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Base](Edit)';
                    break;
                case 'UserAnchorInfo'://用户艺人信息
                    _parm.URL = '/user-manage/anchor-manage/info/' + _parm.ID;
                    _parm.RightCode = '{User}[Anchor](Info)';
                    break;
                case 'UserAdminInfo'://用户管理员信息
                    _parm.URL = '/user-manage/admin-manage/info/' + _parm.ID;
                    _parm.RightCode = '{User}[Admin](Info)';
                    break;
                case 'UserAdminEdit'://用户管理员编辑
                    _parm.URL = '/user-manage/admin-manage/edit/' + _parm.ID;
                    _parm.RightCode = '{User}[Admin](Edit)';
                    break;
                case 'FamilyInfo'://公会信息
                    _parm.URL = '/user-manage/family-manage/info/' + _parm.ID;
                    _parm.RightCode = '{User}[Family](Info)';
                    break;
                case '99'://直播单据
                    _parm.URL = '/order-manage/room-manage/live-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Room](Live-Info)';
                    break;
                case '13'://礼物单据
                    _parm.URL = '/order-manage/room-manage/gift-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Room](Gift-Info)';
                    break;
                case '12'://守护单据
                    _parm.URL = '/order-manage/room-manage/guardian-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Room](Guardian-Info)';
                    break;
                case '16'://喇叭单据
                    _parm.URL = '/order-manage/room-manage/horn-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Room](Horn-Info)';
                    break;
                case '10'://守护单据
                    _parm.URL = '/order-manage/other-manage/vip-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Other](VIP-Info)';
                    break;
                case '11'://坐骑单据
                    _parm.URL = '/order-manage/other-manage/mounts-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Other](Mounts-Info)';
                    break;
                case '22'://艺人罚分单据
                    _parm.URL = '/order-manage/admin-manage/anchorintegral-info/' + _parm.ID;
                    _parm.RightCode = '{Order}[Admin](AnchorIntegral-Info)';
                    break;
                default:
                    $.KOC.Tips.Open('Error', '未知的打开类型', {Timeout: 1000});
                    return;
            }
            if (!_parm.URL) {
                return;
            }
            if (!$.CommonAdmin.Permission.Check(_parm.RightCode)) {
                if (_parm.Info) {
                    try {
                        parent.$.colorbox.close();
                    } catch (ex) {
                        window.close();
                    }
                } else {
                    $.KOC.Tips.Open('Error', '您没有进行此操作的权限', {Timeout: 1000});
                }
                return;
            }
            if (_parm.Parm) {
                _parm.URL += (_parm.URL.indexOf('?') >= 0 ? '&' : '?') + _parm.Parm;
            }
            if (_parm.Info) {
                window.location.href = _parm.URL;
                return;
            }
            if (_parm.Modal) {
                $.CommonAdmin.ModalWindow.Open(_parm.URL, _parm.onEnter, _parm.onClose);
                return;
            }
            if ((_parm.Element ? _parm.Element.nodeName : null) === 'A') {
                $(_parm.Element).attr('href', _parm.URL);
                if (_parm.Event) {
                    $.Common.DefaultPrevented.Call(_parm.Event, _parm.Element);
                }
            } else {
                window.location.href = _parm.URL;
            }
        },
        //endregion
        //region ModalWindow:模态窗体
        ModalWindow: {
            widthMin: 1100,
            widthMax: 1180,
            heightMin: 500,
            heightMax: 765,
            onEnter: null,
            configDef: {
                scrolling: false,
                iframe: true,
                fixed: true,
                closeButton: false,
                overlayClose: false,
                innerWidth: 0,
                innerHeight: 0,
                href: null,
                onClosed: null
            },
            fnEnter: function (close, parms) {
                if (typeof close !== 'boolean') {
                    parms = close;
                    try {
                        close = _ModalData.EnterClose;
                    } catch (ex) {
                        close = false;
                    }
                }
                if (typeof parent.$.CommonBiz.ModalWindow.onEnter == 'function') {
                    parent.$.CommonBiz.ModalWindow.onEnter(parms);
                }
                if (close) {
                    parent.$.colorbox.close();
                }
                return close;
            },
            Open: function (parms, onEnter, onClose) {
                this.configDef.innerHeight = $(window).height() - 40;
                if (this.configDef.innerHeight > this.heightMax) {
                    this.configDef.innerHeight = this.heightMax;
                }
                if (this.configDef.innerHeight < this.heightMin) {
                    this.configDef.innerHeight = this.heightMin;
                }
                this.configDef.innerWidth = $(window).width() - 20;
                if (this.configDef.innerWidth > this.widthMax) {
                    this.configDef.innerWidth = this.widthMax;
                }
                if (this.configDef.innerWidth < this.widthMin) {
                    this.configDef.innerWidth = this.widthMin;
                }
                var _parms = null;
                if (typeof parms === 'string') {
                    this.configDef.href = parms;
                } else {
                    _parms = parms;
                }
                this.onEnter = onEnter;
                if (typeof onClose === 'function') {
                    this.configDef.onClosed = onClose;
                }
                $.colorbox($.extend(true, {}, this.configDef, _parms));
            }
        },
        //endregion
        //region Permission:权限管理
        Permission: {
            Check: function (code) {
                return $.Common.Permission.Check(code, "Admin");
            },
            CheckElement: function (element) {
                return $.Common.Permission.CheckElement(element, "Admin");
            }
        }
        //endregion
    }
});